#include <fhegraphics/RenderContext.h>
#include <GL/gl.h>
#include <GL/glu.h>

namespace fhe
{
  namespace graphics
  {
    RenderContext::RenderContext()
    {
    }
    
    RenderContext::~RenderContext()
    {
    }
    
    void RenderContext::translate( const Vec3d& t )
    {
      glPushMatrix();
      glTranslated( t.x, t.y, t.z );
    }
    
    void RenderContext::rotate( const Rot3d& r )
    {
      glPushMatrix();
    }
    
    void RenderContext::scale( const Vec3d& s )
    {
      glPushMatrix();
      glScaled( s.x, s.y, s.z );
    }
    
    void RenderContext::popTransform()
    {
      glPopMatrix();
    }
    
    void RenderContext::lookAt( const Vec3d& position, const Vec3d& lookAt, const Vec3d& up )
    {
      glPushMatrix();
      gluLookAt( position.x, position.y, position.z, 
                 lookAt.x, lookAt.y, lookAt.z,
                 up.x, up.y, up.z );
    }
    
    void RenderContext::rect( const V& size, const Material& material )
    {
      glColor4d( material.color.r, material.color.g, material.color.b, material.color.a );
      glTranslatef(-0.5,-0.5,-0.5);
      
      glBegin(GL_QUADS);

      //top
      glTexCoord2f(0,0);
      glVertex3f(1,1,0);
      glTexCoord2f(0,1);
      glVertex3f(0,1,0);
      glTexCoord2f(1,1);
      glVertex3f(0,1,1);
      glTexCoord2f(1,0);
      glVertex3f(1,1,1);
      
      //bottom
      glTexCoord2f(0,0);
      glVertex3f(1,0,1);
      glTexCoord2f(0,1);
      glVertex3f(0,0,1);
      glTexCoord2f(1,1);
      glVertex3f(0,0,0);
      glTexCoord2f(1,0);
      glVertex3f(1,0,0);
      
      //front
      glTexCoord2f(1,0);
      glVertex3f(1,1,1);
      glTexCoord2f(0,0);
      glVertex3f(0,1,1);
      glTexCoord2f(0,1);
      glVertex3f(0,0,1);
      glTexCoord2f(1,1);
      glVertex3f(1,0,1);
      
      //back
      glTexCoord2f(0,0);
      glVertex3f(1,0,0);
      glTexCoord2f(0,1);
      glVertex3f(0,0,0);
      glTexCoord2f(1,1);
      glVertex3f(0,1,0);
      glTexCoord2f(1,0);
      glVertex3f(1,1,0);
      
      //right
      glTexCoord2f(0,0);
      glVertex3f(0,1,1);
      glTexCoord2f(0,1);
      glVertex3f(0,1,0);
      glTexCoord2f(1,1);
      glVertex3f(0,0,0);
      glTexCoord2f(1,0);
      glVertex3f(0,0,1);
      
      //left
      glTexCoord2f(0,0);
      glVertex3f(1,1,0);
      glTexCoord2f(0,1);
      glVertex3f(1,1,1);
      glTexCoord2f(1,1);
      glVertex3f(1,0,1);
      glTexCoord2f(1,0);
      glVertex3f(1,0,0);
      
      glEnd();

      glTranslatef(0.5,0.5,0.5);      
    }
  }
}
